﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace 城市天然气管道风险管理与决策系统
{
    public partial class FormStart : Form
    {
        public FormStart()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int sel_index = dataGridView1.CurrentRow.Index;
                GlobalData.default_pipeline = GlobalData.pipelines[sel_index];
                MessageBox.Show("编号" + GlobalData.default_pipeline.Id + "为默认管道", "提示", MessageBoxButtons.OK);
                Form formMain = new FormMain();
                formMain.ShowDialog();
            }
            catch (Exception)
            {
                MessageBox.Show("请选择管道", "提示", MessageBoxButtons.OK);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                var id = int.Parse(textBox1.Text);
                var name = textBox2.Text;
                var start = textBox3.Text;
                var end = textBox4.Text;

                Pipeline pipeline = new Pipeline(id, name, start, end);
                GlobalData.pipelines.Add(pipeline);
                string[] row = new string[4] { id.ToString(), name, start, end };
                dataGridView1.Rows.Add(row);
            }
            catch (Exception)
            {
                MessageBox.Show("新建管道失败！请确认右侧输入格式是否正确","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int sel_index = dataGridView1.CurrentRow.Index;
                dataGridView1.Rows.RemoveAt(sel_index);
                Pipeline old = GlobalData.pipelines[sel_index];

                var id = int.Parse(textBox1.Text);
                var name = textBox2.Text;
                var start = textBox3.Text;
                var end = textBox4.Text;

                old.Id = id;
                old.Name = name;
                old.StartLocation = start;
                old.EndLocation = end;
                Pipeline newp = old;
                GlobalData.pipelines.Add(newp);
                GlobalData.pipelines.RemoveAt(sel_index);
                string[] new_row = new string[4] { id.ToString(), name, start, end };
                dataGridView1.Rows.Add(new_row);
                MessageBox.Show("修改成功", "提示");
            }
            catch (Exception)
            {
                MessageBox.Show("修改管道失败！请确认右侧输入格式是否正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int sel_index = dataGridView1.CurrentRow.Index;
            dataGridView1.Rows.RemoveAt(sel_index);
            GlobalData.pipelines.RemoveAt(sel_index);
            MessageBox.Show("删除成功", "提示");
        }

        private void FormStart_Load(object sender, EventArgs e)
        {
            Stream stream = new FileStream("Data.bin", FileMode.Open, FileAccess.Read, FileShare.None);
            try
            {
                IFormatter formatter = new BinaryFormatter();
                GlobalData.pipelines = (List<Pipeline>)formatter.Deserialize(stream);
                stream.Close();
                foreach (Pipeline pipeline in GlobalData.pipelines)
                {
                    string[] row = new string[4] { pipeline.Id.ToString(), pipeline.Name, pipeline.StartLocation, pipeline.EndLocation };
                    dataGridView1.Rows.Add(row);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("未读取到管道数据");
                stream.Close();
            }
        }

        private void FormStart_FormClosing(object sender, FormClosingEventArgs e)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("Data.bin", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, GlobalData.pipelines);
            stream.Close();
        }
    }
}
